//
//  LoginInfoModel.m
//  XiaoMiao
//
//  Created by jinglian on 2018/2/6.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "LoginInfoModel.h"

static NSString * const kLoginInfoFileName = @"loginInfo.plist";

@implementation LoginInfoModel

// 解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        [self app_decode:aDecoder];
    }
    return self;
}

// 归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [self app_encode:aCoder];
}

+ (BOOL)persistLoginInfo:(LoginInfoModel *)loginInfo {
    //取出沙盒路径
    NSString *path = [LoginInfoModel loginInfoAllPath];
    
    return [NSKeyedArchiver archiveRootObject:loginInfo toFile:path];
}

+ (LoginInfoModel *)getLoginInfo; {
    // 取出沙盒路径
    NSString *path = [LoginInfoModel loginInfoAllPath];
    
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

+ (void)clearLoginInfo {
    NSError *error;
    NSFileManager *defauleManager = [NSFileManager defaultManager];
    [defauleManager removeItemAtPath:[LoginInfoModel loginInfoAllPath] error:&error];
    NSMutableDictionary *headparam = [NSMutableDictionary dictionary];
    headparam[@"deviceToken"] = [AppGlobal getDeviceToken];
    [AppNetwork configCommonHttpHeaders:headparam];
}

+ (NSString *)loginInfoAllPath {
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:kLoginInfoFileName];
    
    return path;
}

@end
